package com.jndk.netty.json;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

/**
 * Created by Sheng.Fan on 2015-11-11.
 */
public class JsonUtil {
    private static org.codehaus.jackson.map.ObjectMapper mapper = null;

    public static String toJson(Object obj) throws Exception {
        if (mapper == null)
            mapper = new org.codehaus.jackson.map.ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        //mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return mapper.writeValueAsString(obj);
    }

    public static <T> T fromJson(String json, Class<T> clazz) throws Exception {
        if (mapper == null)
            mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
        mapper.setSerializationInclusion(Inclusion.NON_NULL);
        return mapper.readValue(json, clazz);
    }
}
